package model

import (
	"context"

	"github.com/Masterminds/squirrel"
	"github.com/zeromicro/go-zero/core/stores/cache"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

var _ UmsResourceModel = (*customUmsResourceModel)(nil)

type (
	// UmsResourceModel is an interface to be customized, add more methods here,
	// and implement the added methods in customUmsResourceModel.
	UmsResourceModel interface {
		umsResourceModel
		Trans(ctx context.Context, fn func(ctx context.Context, session sqlx.Session) error) error
		RowBuilder() squirrel.SelectBuilder
		CountBuilder(field string) squirrel.SelectBuilder
		FindAll(ctx context.Context, rowBuilder squirrel.SelectBuilder) ([]*UmsResource, error)
		FindCount(ctx context.Context, CountBuilder squirrel.SelectBuilder) (int64, error)
	}

	customUmsResourceModel struct {
		*defaultUmsResourceModel
	}
)

// NewUmsResourceModel returns a model for the database table.
func NewUmsResourceModel(conn sqlx.SqlConn, c cache.CacheConf) UmsResourceModel {
	return &customUmsResourceModel{
		defaultUmsResourceModel: newUmsResourceModel(conn, c),
	}
}
func (m *defaultUmsResourceModel) FindByPageParam(ctx context.Context, rowBuilder squirrel.SelectBuilder, pageNum, pageSize int64) ([]*UmsResource, error) {
	query, values, err := rowBuilder.ToSql()
	if err != nil {
		return nil, err
	}
	var resp []*UmsResource
	err = m.QueryRowsNoCacheCtx(ctx, &resp, query, values...)
	switch err {
	case nil:
		return resp, nil
	default:
		return nil, err
	}
}
func (m *defaultUmsResourceModel) FindCount(ctx context.Context, CountBuilder squirrel.SelectBuilder) (int64, error) {
	query, values, err := CountBuilder.ToSql()
	if err != nil {
		return 0, err
	}
	var resp int64
	err = m.QueryRowNoCacheCtx(ctx, &resp, query, values...)
	switch err {
	case nil:
		return resp, nil
	default:
		return 0, err
	}
}
func (m *defaultUmsResourceModel) FindAll(ctx context.Context, rowBuilder squirrel.SelectBuilder) ([]*UmsResource, error) {
	query, values, err := rowBuilder.ToSql()
	if err != nil {
		return nil, err
	}
	var resp []*UmsResource
	err = m.QueryRowsNoCacheCtx(ctx, &resp, query, values...)
	switch err {
	case nil:
		return resp, nil
	default:
		return nil, err
	}
}

// export logic
func (m *defaultUmsResourceModel) Trans(ctx context.Context, fn func(ctx context.Context, session sqlx.Session) error) error {
	return m.TransactCtx(ctx, func(ctx context.Context, session sqlx.Session) error {
		return fn(ctx, session)
	})

}

// export logic
func (m *defaultUmsResourceModel) RowBuilder() squirrel.SelectBuilder {
	return squirrel.Select(umsResourceRows).From(m.table)
}

// export logic
func (m *defaultUmsResourceModel) CountBuilder(field string) squirrel.SelectBuilder {
	return squirrel.Select("COUNT(" + field + ")").From(m.table)
}

// export logic
func (m *defaultUmsResourceModel) SumBuilder(field string) squirrel.SelectBuilder {
	return squirrel.Select("IFNULL(SUM(" + field + "),0)").From(m.table)
}
